/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_CONSOLE_H
#define XERN_CONSOLE_H

#include <map>
#include <set>

#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/uuid/uuid_generators.hpp>

#include "IConsole.hpp"

#include "Vector.hpp"
#include "Quaternion.hpp"

namespace Xern
{
	using boost::uuids::nil_uuid;

	/*
	 * Console - A console
	 */
	class Console: public IConsole
	{
	public:
		Console();
		virtual ~Console();

		virtual void Print(const char *fmt, ...);
		virtual int ExecCommand(const char *cmd);
		virtual int ExecCommand(int argc, const char *argv[]);
		virtual bool ContainsVariable(const char *varname);
		virtual bool ContainsCommand(const char *cmd);
		virtual bool SetConsoleVariable(const char *var, const char *value);

		virtual bool RegisterVariable(boost::shared_ptr<IConsoleVariable> var);
		virtual bool RegisterCommand(boost::shared_ptr<IConsoleCommand> cmd);
		virtual bool RegisterSubConsole(boost::shared_ptr<IConsole> subcon);

	protected:
		typedef std::map<std::string, boost::shared_ptr<IConsoleVariable> >
				variables_type;
		typedef std::map<std::string, boost::shared_ptr<IConsoleCommand> >
				commands_type;
		typedef std::set<boost::shared_ptr<IConsole> > subconsoles_type;

		mutable boost::mutex consoleMutex;
		variables_type variables;
		commands_type commands;
		subconsoles_type subconsoles;
	};

	/*
	 * XernConsoleVariable - A console variable implementation
	 */
	class ConsoleVariable: public IConsoleVariable
	{
	public:
		ConsoleVariable(const char *name, const char *defaultValue,
				const char *description = "");
		virtual ~ConsoleVariable();

		virtual const char *GetName() const;
		virtual const char *GetDescription() const;
		virtual const char *GetValue() const;
		virtual void SetValue(const char *value);

		int GetIntValue() const
		{
			const char *value = GetValue();
			return value ? atoi(value) : 0;
		}

		float GetFloatValue() const
		{
			const char *value = GetValue();
			return value ? atof(value) : 0.0f;
		}

		Vector3 GetVectorValue() const
		{
			Vector3 ret(0);
			const char *value = GetValue();
			if (value)
				sscanf(value, "%f %f %f", &ret.x, &ret.y, &ret.z);
			return ret;
		}

		Quaternion GetQuaternionValue() const
		{
			Quaternion ret;
			const char *value = GetValue();
			if (value)
				sscanf(value, "%f %f %f %f", &ret.w, &ret.x, &ret.y, &ret.z);
			return ret;
		}

		uuid GetUuidValue() const
		{
			boost::uuids::string_generator gen;
			const char *value = GetValue();
			return value ? gen(value) : nil_uuid();
		}

	private:
		const char *name_;
		const char *description_;
		std::string value_;
	};

	/*
	 * ConsoleCommand - A console command implementation
	 */
	class BaseConsoleCommand: public IConsoleCommand
	{
	public:

		BaseConsoleCommand(const char *name, const char *description = "")
		{
			this->name = name;
			this->description = description;
		}

		virtual ~BaseConsoleCommand()
		{
		}

		virtual int Main(int argc, const char *argv[])=0;

		virtual const char *GetName() const
		{
			return name;
		}

		virtual const char *GetDescription() const
		{
			return description;
		}

	private:
		const char *name;
		const char *description;
	};

	namespace detail
	{
	// Console command delegate
	template<class T>
	class MemberConsoleCommand: public BaseConsoleCommand
	{
	public:
		typedef int (T::*cmdpfun_t)(int argc, const char *argv[]);

		MemberConsoleCommand(T* obj, cmdpfun_t function, const char *name,
				const char *description = "") :
			BaseConsoleCommand(name, description)
		{
			cmd_function = function;
			object = obj;
		}

		virtual int Main(int argc, const char *argv[])
		{
			return (object->*cmd_function)(argc, argv);
		}

	protected:
		cmdpfun_t cmd_function;
		T* object;
	};
	} // namespace detail

	template<typename A, typename B>
	boost::shared_ptr<detail::MemberConsoleCommand<B> > ConsoleCommand(A function,
			B *self, const char *name, const char *desc = "")
	{
		return boost::shared_ptr<detail::MemberConsoleCommand<B> >(
				new detail::MemberConsoleCommand<B>(self, function, name, desc));
	}

} // namespace Xern

#endif //XERN_CONSOLE_HPP
